package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.PageDTO;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.admin.DishMapper;
import com.sky.mapper.admin.SetmealDishMapper;
import com.sky.mapper.admin.SetmealMapper;
import com.sky.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;

    // 套餐分页搜索
    @Override
    public PageDTO<SetmealDTO> pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        // 设置分页条件
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        // 设置查询条件并查询
        Page<SetmealDTO> pages = setmealMapper.pageQuery();
        // 提取分页查询结果
        long total = pages.getTotal();
        List<SetmealDTO> result = pages.getResult();

        return new PageDTO(total, result);
    }

    // 新建套餐
    @Override
    @Transactional
    public void save(SetmealDTO setmealDTO) {
        // 向套餐表中插入数据,需要返回套餐的id
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.insert(setmeal);
        // 为 SetmealDish 数据库表实体添加套餐id
        Long setmealId = setmeal.getId();
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmealId);
        });
        // 向 套餐--菜品表 中插入数据
        setmealDishMapper.insertBatch(setmealDishes);
    }

    // 修改套餐
    @Override
    @Transactional
    public void update(SetmealDTO setmealDTO) {
        // 修改套餐表数据
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.update(setmeal);
        // 修改 套餐--菜品表 数据
        // 先删除之前的 套餐--菜品表 数据
        Long setmealId = setmealDTO.getId();
        setmealDishMapper.deleteBySetmealId(setmealId);
        // 插入新的 套餐--菜品表 数据
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmealId);
        });
        setmealDishMapper.insertBatch(setmealDishes);
    }

    // 根据套餐id查询套餐
    @Override
    @Transactional
    public SetmealDTO getBySetmealId(long setmealId) {
        // 获取套餐信息
        Setmeal setmeal = setmealMapper.getBySetmealId(setmealId);
        // 获取套餐中的菜品信息
        List<SetmealDish> setmealDishList = setmealDishMapper.getBySetmealId(setmealId);
        // 封装 SetmealDTO
        SetmealDTO setmealDTO = new SetmealDTO();
        BeanUtils.copyProperties(setmeal, setmealDTO);
        setmealDTO.setSetmealDishes(setmealDishList);

        return setmealDTO;
    }

    // 根据id起售停售套餐
    @Override
    public void StratOrStop(Integer status, long id) {
        // 如果套餐中包含未起售的套餐，那么套餐无法设置未起售
        if(status == StatusConstant.ENABLE){
            // 1.根据套餐id获得菜品id
            List<Long> dishIds = setmealDishMapper.getDishIdsBySetmealId(id);
            // 2.根据菜品id查看菜品状态
            List<Dish> dishes = dishMapper.selectBatchIds(dishIds);
            for (Dish dish : dishes) {
                if(dish.getStatus() == StatusConstant.DISABLE ){
                    // 套餐无法设置未起售
                    throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                }
            }
        }
        setmealMapper.StartOrStop(status, id);
    }

    // 根据套餐id批量删除套餐
    @Override
    @Transactional
    public void deleteBatch(List<Long> setmealIds) {
        // 判断套餐售卖状态
        List<Setmeal> setmealList = setmealMapper.getStatusBySetmealId(setmealIds);
        for (Setmeal setmeal : setmealList) {
            if(setmeal.getStatus() == StatusConstant.ENABLE){
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }
        // 从套餐表中删除套餐
        setmealMapper.deleteBatch(setmealIds);
        // 从 套餐--菜品表 中删除套餐相关数据
        setmealDishMapper.deleteBatch(setmealIds);
    }
}